//
//  CMDBTagCollectionCell.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/15.
//  Copyright © 2017年 CMDB. All rights reserved.
//

/*
 let attriteTitle = "标签"
 
 let attributeBtn = UIButton(title: attriteTitle, borderColor: UIColor.Hex(0xff6d00), titleSizeFont: CMDB_12_FONT, isShowBorder: true)
 
 attributeBtn.setTitleColor(word87Color, forState: .Normal)
 
 let size = attriteTitle.suitToSize(MaxSize, fontSize: 12, lineSpacing: 1)
 
 let x : CGFloat = index > 0 ? KleftMargin + CGRectGetMaxX(attributesBtns[index - 1].frame): 0.0
 
 attributeBtn.frame = CGRect(x: x , y: 0, width: size.width + 20, height: size.height + 5)
 
 attributesBtns.append(attributeBtn)
 
 tagsView.addSubview(attributeBtn)

 */

import UIKit

class CMDBTagCollectionCell: CMDBBaseCollectionViewCell {
    
    var tagLabel : UILabel!
    
    var deleteBtn : UIButton!
    
    var tagModel : CMDBTagModel?{
        
        didSet{
            
            guard let tagModel = tagModel else { return }
            
            tagLabel.text = tagModel.name
            
            layer.borderColor = UIColor.colorWithHexString(tagModel.borderColor).CGColor
            
            let textC    = tagModel.textColor.isEmpty ? word87Color : UIColor.colorWithHexString(tagModel.textColor)
            
            let backC    = tagModel.bgColor.isEmpty ?  UIColor.whiteColor() : UIColor.colorWithHexString(tagModel.bgColor)
            
            if tagModel.isSelected {
                
                backgroundColor = UIColor.Hex(0xff6d00)
                
                tagLabel.textColor = UIColor.whiteColor()
                
            }else{
                
                backgroundColor = tagModel.bgColor.isEmpty ? UIColor.whiteColor() : backC
                
                tagLabel.textColor = tagModel.textColor.isEmpty ? word87Color : textC
                
            }
            
        }
        
    }

    override func setupCollectionCell() {
        super.setupCollectionCell()
        
        layer.cornerRadius  = 3
        
        layer.borderWidth = 1
        
    }
    
    override func buildCollectionCell() {
        super.buildCollectionCell()
        
        setTagCell()
        
    }
}

extension CMDBTagCollectionCell{
    
    func setTagCell(){
        tagLabel = UILabel(frame: self.bounds)
        
        tagLabel.textAlignment = .Center
        
        tagLabel.font = CMDB_12_FONT
        
        contentView.addSubview(tagLabel)
        
        deleteBtn = UIButton(frame: CGRect(x: CGRectGetWidth(frame) - 7, y: -7, width: 11, height: 11))
        
        deleteBtn.setImage(UIImage(named: "作品_删除"), forState: .Normal)
        
        deleteBtn.sizeToFit()
        
        deleteBtn.hidden = true
        
        contentView.addSubview(deleteBtn)
        
        contentView.bringSubviewToFront(deleteBtn)
    }
    
}

